//
//  LauchViewController.m
//  HWSGW
//
//  Created by avachen on 15/11/10.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "LauchViewController.h"
#import "UIViewExt.h"

@interface LauchViewController ()

@end

static NSInteger _index = 0;

@implementation LauchViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *bgImg = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bgImg.image = [UIImage imageNamed:@"Default.png"];
    [self.view addSubview:bgImg];
    
    
    [self _loadImages];
    [self _startAnimation];
    
    
}
- (void)_loadImages{
    int count = 16;
    
    CGFloat width = kScreenWidth / 4;
    CGFloat height = kScreenHeight / 6;
    
    
    _imgArray = [[NSMutableArray alloc] initWithCapacity:count];
    
    int x = 0, y = 0;
    
    for (int i = 0; i < count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        NSString *imgName = [NSString stringWithFormat:@"%d.png" , i+1];
        imgView.image = [UIImage imageNamed:imgName];
        
        imgView.alpha = 0;
        [self.view addSubview:imgView];
        [_imgArray addObject:imgView];
        
        
        if (i <= 3) {
            y = 0;
            x = i * width;
        }else if (i <= 7){
            x = 3 * width;
            y = (i - 3) * height;
        }else if (i <= 11){
            y = kScreenHeight - height;
            x = (11 - i) * width;
        }else if (i < 16){
            x = 0;
            y = (16 - i) * height;
            
        }
        
        imgView.origin = CGPointMake(x, y);
        
        
        
    }
    
}
//显示imgView
- (void)_startAnimation{
    
    if (_index >= _imgArray.count) {
        [self showMain];
        return;
        
    }
    UIImageView *imgView = _imgArray[_index];
    
    imgView.alpha = 0;
    
    [UIView animateWithDuration:0.2 animations:^{
        imgView.alpha = 1;
        
    }];
    
    _index++;
    
    [self performSelector:@selector(_startAnimation) withObject:nil afterDelay:0.2];
    
    
    
}
//显示首页
- (void)showMain{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    UIViewController *viewController = [storyBoard instantiateInitialViewController];
    
    UIWindow *window = self.view.window;
    window.rootViewController = viewController;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
